package com.liuwei.spring.cloud.common.utils;

import cn.hutool.core.util.StrUtil;
import io.krakens.grok.api.Grok;
import io.krakens.grok.api.GrokCompiler;
import io.krakens.grok.api.Match;
import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@SuppressWarnings("ALL")
@Slf4j
public class StringHelper {

	/**
	 * 获取随机字符串
	 *
	 * @param num
	 * @return
	 */
	public static String getRandomString(Integer num) {
		return getRandomString(num,StringPool.ALPHA+StringPool.NUMBER);
	}

	public static String getRandomString(Integer num,String range) {
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < num; i++) {
			int number = random.nextInt(range.length());
			sb.append(range.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 获取随机字符串
	 *
	 * @param num
	 * @return
	 */
	public static String getRandomNum(Integer num) {
		String base = StringPool.NUMBER;
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < num; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 右补位，左对齐
	 *
	 * @param oriStr   原字符串
	 * @param len      目标字符串长度
	 * @param fillChar 补位字符
	 * @return 目标字符串
	 */
	public static String padRight(String oriStr, int len, char fillChar) {
		String str = StringPool.EMPTY;
		int strlen = oriStr.length();
		if (strlen < len) {
			for (int i = 0; i < len - strlen; i++) {
				str = str + fillChar;
			}
		}
		str = str + oriStr;
		return str;
	}

	/**
	 * 左补位，右对齐
	 *
	 * @param oriStr   原字符串
	 * @param len      目标字符串长度
	 * @param fillChar 补位字符
	 * @return 目标字符串
	 */
	public static String padLeft(String oriStr, int len, char fillChar) {
		int strlen = oriStr.length();
		String str = StringPool.EMPTY;
		if (strlen < len) {
			for (int i = 0; i < len - strlen; i++) {
				str = str + fillChar;
			}
		}
		str = oriStr + str;
		return str;
	}


	public static String getStackTrace(Throwable throwable) {
		StringWriter sw = new StringWriter();
		throwable.printStackTrace(new PrintWriter(sw,true));
		return sw.toString();
	}

	/**
	 * https://blog.csdn.net/qq_39380838/article/details/102498506
	 * https://blog.csdn.net/wenqingping5/article/details/118546032
	 *
	 * @param logMsg
	 * @param pattern
	 * @return
	 */
	public static Map<String, Object> grok(String logMsg, String pattern) {
		GrokCompiler grokCompiler = GrokCompiler.newInstance();
		// 进行注册, registerDefaultPatterns()方法注册的是Grok内置的patterns
		grokCompiler.registerDefaultPatterns();
		// 添加自定的正则表达式，解析日期（例如：2021-07-07 13:45:39.210）
		grokCompiler.register("DATETIME", "[0-9,\\.\\-: ]+");

		/* 传入自定义的pattern, 会从已注册的patterns里面进行配对,
		 * 例如： %{DATETIME:datetime}
		 * 配对成功后, 会在match时按照固定的解析格式将解析结果存入map中, 此处datetime作为输出的key
		 */
		//Grok grok = grokCompiler.compile("%{DATETIME:datetime} %{LOGLEVEL:level} %{INT:pid} --- \\[%{NOTSPACE:thread}\\] %{NOTSPACE:classed}\\s*:%{GREEDYDATA:message}");
		//String logMsg = "2021-07-07 13:45:39.210  INFO 16100 --- [SpringContextShutdownHook] com.zaxxer.hikari.HikariDataSource       : Shutdown completed.";
		Grok grok = grokCompiler.compile(pattern);

		// 通过match()方法进行匹配, 对log进行解析, 按照指定的格式进行输出
		Match grokMatch = grok.match(logMsg);

		// 获取结果
		Map<String, Object> resultMap = grokMatch.capture();
		return resultMap;
	}

}
